package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.TradeArea;
import xyz.hubery.dto.TradeAreaAndMarketDTO;
import xyz.hubery.dto.TradeAreaDTO;
import xyz.hubery.dto.TradeMarketDTO;
import xyz.hubery.feign.TradeAreaServiceFeign;
import xyz.hubery.model.R;
import xyz.hubery.service.TradeAreaService;
import xyz.hubery.service.UserFavoriteMarketService;
import xyz.hubery.vo.TradeAreaMarketVo;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tradeAreas")
public class TradeAreaController implements TradeAreaServiceFeign {

    @Autowired
    private TradeAreaService tradeAreaService;


    @GetMapping
    @ApiOperation("分页查询交易区域")
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<Page<TradeArea>> findPage( Page<TradeArea> page, @RequestParam(required = false) String name, @RequestParam(required = false) Byte status) {
        Page<TradeArea> areaPage = tradeAreaService.findPage(page, name, status);
        return R.ok(areaPage);
    }

    @GetMapping("/all")
    @ApiOperation("查询所有交易区域")
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<List<TradeArea>> findAll(Byte status) {
        List<TradeArea> list = tradeAreaService.list(new LambdaQueryWrapper<TradeArea>().eq(status != null, TradeArea::getStatus, status).orderByAsc(TradeArea::getSort));
        return R.ok(list);
    }

    @PostMapping("/status")
    @ApiOperation("修改交易区域状态")
    @PreAuthorize("hasAuthority('trade_area_update')")
    public R updateStatus(@RequestBody  TradeArea tradeArea) {
        if(tradeArea.getId() == null || tradeArea.getStatus() == null) {
            return R.fail("参数错误");
        }
        boolean b = tradeAreaService.updateById(tradeArea);
        return b?R.ok():R.fail("修改失败");
    }

    @PatchMapping
    @ApiOperation("修改交易区域")
    @PreAuthorize("hasAuthority('trade_area_update')")
    public R update(@RequestBody @Validated TradeArea tradeArea) {
        boolean b = tradeAreaService.updateById(tradeArea);
        return b?R.ok():R.fail("修改失败");
    }

    @PostMapping
    @ApiOperation("新增交易区域")
    @PreAuthorize("hasAuthority('trade_area_create')")
    public R add(@RequestBody @Validated TradeArea tradeArea) {
        boolean b = tradeAreaService.save(tradeArea);
        return b?R.ok():R.fail("新增失败");
    }

    @GetMapping("/markets")
    @ApiOperation(value = "查询交易区域,以及区域下的市场")
    public R<List<TradeAreaMarketVo>> getTradeAreaMarkets(){
        List<TradeAreaMarketVo> tradeAreaMarketVos =  tradeAreaService.findTradeAreaMarket();
        return R.ok(tradeAreaMarketVos) ;
    }

    @GetMapping("/market/favorite")
    @ApiOperation("查询用户收藏的交易市场")
    public R<List<TradeAreaMarketVo>> getUserFavoriteMarkets(){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<TradeAreaMarketVo> tradeAreaMarketVos =  tradeAreaService.getUserFavoriteMarkets(userId);
        return R.ok(tradeAreaMarketVos) ;
    }


    /**
     * 获取所有的交易区域及其管辖的交易市场
     * @return
     */
    @GetMapping("/getAraeAndMarkets")
    @Override
    public List<TradeAreaAndMarketDTO> listAreaAndMarkets() {
        return tradeAreaService.listAreaAndMarkets();
    }
}
